package com.hzhc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.ProductName;
import com.hzhc.mapper.ProductNameMapper;
import com.hzhc.service.ProductNameService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.ProductNameVo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 铁路品名表 服务实现类
 * </p>
 *
 * @author shizj
 * @since 2024-08-30
 */
@Service
public class ProductNameServiceImpl extends ServiceImpl<ProductNameMapper, ProductName> implements ProductNameService {

    @Override
    public List<ProductNameVo> getList(Page<ProductNameVo> page, ProductNameVo vo) {
        IPage<ProductNameVo> iPage = baseMapper.selectProductNameVoPage(page, vo);
        return iPage.getRecords();
    }

    @Override
    public ResultUtil<T> addRet(ProductName productName) {
        boolean b = save(productName);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> updateRet(ProductName productName) {
        boolean b = updateById(productName);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> deleteRet(ProductName productName) {
        ProductName name = baseMapper.selectById(productName.getId());
        name.setDisable(true);
        boolean b = updateById(name);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ProductName checkTradeName(String field, String tradeName, Long id) {
        QueryWrapper<ProductName> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("disable",false);
        queryWrapper.eq(field,tradeName);
        if (id!=null){
            queryWrapper.ne("id",id);
        }
        ProductName name = baseMapper.selectOne(queryWrapper);
        return name;
    }
}
